package com.global.gbinventory.barang.tablemodel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.global.gbinventory.barang.model.Subgrup;
import com.global.gbinventory.barang.util.TYPE;
import com.global.gbinventory.barang.util.DBMaster;
import com.global.gbinventory.barang.util.CONSTANT;
import com.global.gbinventory.main.model.GBTableModel;

public class SubgrupTableModel extends GBTableModel{

	private static final long serialVersionUID = 6767172641074736533L;
	
	private TYPE type;
	private DBMaster dbMaster;
	private List<Subgrup> subgrups;
	private int grupId;
	private List<Integer> editedRow;
	
	public SubgrupTableModel(DBMaster dbMaster, List<Subgrup> subgrups, int grupId){
		this.dbMaster = dbMaster;
		this.subgrups = subgrups;
		this.grupId = grupId;
		type = TYPE.SUBGRUPs;
		editedRow = new ArrayList<Integer>();
		initSubgrupTableData();
	}
	
	/**
	 * initialize the table model using the data from grupLists
	 */
	private void initSubgrupTableData(){
		List<Object[]> tmData = new ArrayList<Object[]>();
		Object[] tmCols = null;
		
		for(Subgrup sg : subgrups){
			//add Subgrup data to table model
			String nama = sg.getNama();
			tmCols = new Object[type.getColumnCount()];
			tmCols[CONSTANT.SUBGRUP_TM_NAMA] = nama;
			tmData.add(tmCols);
		}
		
		setColumnNames(type.getColumnNames());
		setData(tmData);
		
	}
	
	public int getLastRow(){
		return getRowCount() - 1;
	}
	
	public void addNewSubgrup(Object[] tuple){
		addRow(tuple);
	}
	
	public void save(){
		
		saveNewSubgrup();
		saveEditedSubgrup();
		
	}
	
	private void saveNewSubgrup(){
		int totalOldRows = subgrups.size();
		int totalActualRows = getRowCount();
		int id = getNewId();
		for(int i = totalOldRows; i < totalActualRows; i++){
			String nama = getValueAt(i, CONSTANT.SUBGRUP_TM_NAMA).toString();
			
			try {
				Object[] tuple = new Object[CONSTANT.SUBGRUP_DB_TOTAL_COLUMN];
				tuple[CONSTANT.SUBGRUP_DB_NAMA - 1] = nama; //minus 1 because subgrup_id
				tuple[CONSTANT.SUBGRUP_DB_GRUP - 1] = grupId; //minus 1 because subgrup_id
				dbMaster.addNewTuple(type, tuple);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			Subgrup sg = new Subgrup(id, nama, grupId);
			subgrups.add(sg);
		}
	}
	
	private void saveEditedSubgrup(){
		for(Integer i : editedRow){
			String nama = getValueAt(i, CONSTANT.SUBGRUP_TM_NAMA).toString();
			Subgrup sg = subgrups.get(i);
			int subgrupId = sg.getId();
			
			try {
				Object[] tuple = new Object[CONSTANT.SUBGRUP_DB_TOTAL_COLUMN - 1]; //minus 1 because grupId never be updated
				tuple[CONSTANT.SUBGRUP_DB_NAMA - 1] = nama; //minus 1 because subgrup_id
				dbMaster.updateTuple(type, subgrupId, tuple);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			sg.setNama(nama);
		}
		editedRow.clear();
	}
		
	private int getNewId(){
		return dbMaster.getLastPkFromDB(type) + 1;
	}
	
	public Subgrup getSubgrup(int index){
		return subgrups.get(index);
	}
	
	public void cancel(){
		int actualRow = subgrups.size();
		int count = getRowCount() - actualRow;
		while (count > 0) {
			removeRow(actualRow);
			count--;
		}
		
		for(Integer i : editedRow){
			String nama = subgrups.get(i).getNama();
			setValueAt(nama, i, CONSTANT.SUBGRUP_TM_NAMA);
		}
		editedRow.clear();
	}
	
	public void updateEditedRow(int row){
		if(row < subgrups.size() && row > -1 && !editedRow.contains(row)){
			editedRow.add(row);
		}
	}
	
	@Override
    public boolean isCellEditable(int row, int col) {
    	return true;
    }
}
